//------------------------------------------------------------------------------
//  <copyright file="GamePadEmulatorModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Robotics.Interaction.Visualization
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Windows;
    using Microsoft.Robotics.Interaction.Runtime.Input;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// This model agent class prepares the classGamePadEmulatorModelAgent.
    /// </summary>
    [DataContract]
    public sealed class GamePadEmulatorModelAgent : ModelAgent<GamePadAgentMessage, GamePadEmulatorModel>
    {
        /// <summary>
        /// Name of the game pad emulator agent
        /// </summary>
        [DataMember(Name = "GamePadEmulator")]
        private string gamePadEmulatorAgentName = default(string);

        /// <summary>
        /// game pad message forwarder.
        /// </summary>
        private IForwarder<GamePadAgentMessage> gamePadStateForwarder;

        /// <summary>
        /// Initializes a new instance of the GamePadEmulatorModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="gamePadEmulatorAgent">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>
        public GamePadEmulatorModelAgent(
            string name,
            IProducer<GamePadAgentMessage> gamePadEmulatorAgent,
            int maxUpdateIntervalMilliseconds)
            : base(name, gamePadEmulatorAgent.Name, maxUpdateIntervalMilliseconds)
        {
            this.gamePadEmulatorAgentName = gamePadEmulatorAgent.Name;
        }

        /// <summary>
        /// Initializes any fields not provided in the manifest.
        /// </summary>
        /// <param name="locator">Not used.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            IAgentHost gamePadEmulatorAgent = locator.GetAgent(this.gamePadEmulatorAgentName);
            this.gamePadStateForwarder = gamePadEmulatorAgent.GetControlPort<GamePadAgentMessage>();
            this.Model.GamePadEmulatorDelegate = (message) => this.gamePadStateForwarder.Post(message);
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="state">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(GamePadAgentMessage state)
        {
            // There is nothing to update in the dispay.
            return true;
        }
    }
} 
